package com.weeego.web.action.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sql.weeego.bean.base.CenterUser;
import com.weeego.util.file.MakeDir;
import com.weeego.util.image.ImageEdit;
import com.weeego.web.base.FormatDate;

public class BaseUploadAction extends ActionSupport {
	protected final Logger logger = Logger.getLogger(getClass());
	private String newsImgUrl;
	private String comeform;
	private String callFun="callback";
	private File upload_file;
	private String upload_fileContentType;
	private String upload_fileFileName;
	private String directory;

	private String savePath;

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	private String commondResult = "";

	public String getCommondResult() {
		return commondResult;
	}

	public void setCommondResult(String commondResult) {
		this.commondResult = commondResult;
	}

	public String action() {

		CenterUser _CenterUser = new CenterUser();
		Map session = ActionContext.getContext().getSession();

		_CenterUser = (CenterUser) session.get("user");

		try {
			String currPath = "";
			FileInputStream in = new FileInputStream(upload_file);

			currPath = ServletActionContext.getRequest().getRealPath("/");
			currPath = currPath + getSavePath() + _CenterUser.getId();
			MakeDir _MakeDir = new MakeDir();
			_MakeDir.makeDir(currPath);
			FormatDate _FormatDate = new FormatDate();
			Random random = new Random();
			String _temp = _FormatDate.getNowTime() + random.nextInt(9999999);

			upload_fileFileName = _temp;

			File diskFile = new File(currPath + "/" + upload_fileFileName);
			diskFile.createNewFile();
			FileOutputStream out = new FileOutputStream(diskFile);
			byte buffer[] = new byte[1024];
			while ((in.read(buffer)) != -1) {
				out.write(buffer);
			}
			in.close();
			out.close();

		} catch (Exception e) {
			logger.error("====upload file has some error: ", e);
		}
		newsImgUrl = getSavePath() + _CenterUser.getId() + "/"
				+ upload_fileFileName;
		this.commondResult=newsImgUrl;
		return SUCCESS;
	}

	public File getUpload_file() {
		return upload_file;
	}

	public void setUpload_file(File upload_file) {
		this.upload_file = upload_file;
	}

	public String getUpload_fileContentType() {
		return upload_fileContentType;
	}

	public void setUpload_fileContentType(String upload_fileContentType) {
		this.upload_fileContentType = upload_fileContentType;
	}

	public String getUpload_fileFileName() {
		return upload_fileFileName;
	}

	public void setUpload_fileFileName(String upload_fileFileName) {
		this.upload_fileFileName = upload_fileFileName;
	}

	public String getNewsImgUrl() {
		return newsImgUrl;
	}

	public void setNewsImgUrl(String newsImgUrl) {
		this.newsImgUrl = newsImgUrl;
	}

	public String getComeform() {
		return comeform;
	}

	public void setComeform(String comeform) {
		this.comeform = comeform;
	}

	public String getCallFun() {
		return callFun;
	}

	public void setCallFun(String callFun) {
		this.callFun = callFun;
	}

	public String getDirectory() {
		return directory;
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

}
